<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        .box1 {
            width: 100px;
            height: 100px;
            margin: 50px auto;
            border: 2px solid red;
            left: 100px;
            position: absolute;
        }
        
        .box2 {
            width: 50px;
            height: 50px;
            background-color: seagreen;
            margin-top: 20px;
            margin-left: 20px;
        }
    </style>
</head>

<body>
    <div class="box1">
        <div class="box2"></div>
    </div>
    <script>
        var box = document.querySelector('.box1');
        var box2 = document.querySelector('.box2');
        console.log('box2---', box2.offsetLeft);
        console.log('box2---', box2.offsetTop);

        box.offsetLeft;
        console.log(box.offsetLeft); //元素距离父元素的上左距离
        console.log(box.offsetTop);
        box.onclick = function(e) {
            var e = event;
            e.clientX;
            console.log(e.clientX); //距离当前距离浏览器左上边框的距离,也就是当前鼠标的位置
            console.log(e.clientY);

            先用鼠标相对于浏览器的宽度减去当前元素离左边的宽度得到鼠标到元素左边的宽度,
            得到后再用新的位置减去上面得到的宽度得到了现在的left.
        }
        box
    </script>
</body>

</html>